package com.binhthuan.catechist.server.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.binhthuan.catechist.server.ErrorObject;
import com.binhthuan.catechist.server.ResponseObject;
import com.binhthuan.catechist.server.bus.VenueBUS;
import com.binhthuan.catechist.server.domain.Venue;
import com.binhthuan.catechist.shared.constant.Parameter;

public class VenueServlet extends AbstractServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -2296376368913712790L;

    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        addVenue(request, response);
    }

    @Override
    protected void fetch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameter(Parameter.ID) == null) {
            getAllVenues(request, response);
        } else {
            getVenue(request, response);
        }
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response, boolean confirmation) throws ServletException,
            IOException {
        deleteVenue(request, response, confirmation);
    }

    @Override
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        updateVenue(request, response);
    }

    private void addVenue(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter(Parameter.NAME);

        try {
            Venue obj = new Venue();
            obj.setName(name);
            
            VenueBUS bus = new VenueBUS();
            if (bus.getVenueByName(name.trim()) != null) {
                ErrorObject error = new ErrorObject(STATUS_VALIDATION_ERROR);
                error.addError(Parameter.NAME, getMessagesFactory().duplicatedName());
                sendResponse(error.toJson());
                return;
            }
            if (bus.save(obj)) {
                sendResponse(new ResponseObject(obj).toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void getAllVenues(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            VenueBUS bus = new VenueBUS();
            List<Venue> venues = bus.getAllVenues();

            sendResponse(new ResponseObject(venues).toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void getVenue(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter(Parameter.ID);

        try {
            int venueId = Integer.parseInt(id);

            VenueBUS bus = new VenueBUS();
            Venue obj = bus.getVenueById(venueId);

            sendResponse(new ResponseObject(obj).toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void deleteVenue(HttpServletRequest request, HttpServletResponse response, boolean confirmation) throws IOException {
        String venueId = request.getParameter(Parameter.ID);
        try {
            int id = Integer.parseInt(venueId);

            VenueBUS bus = new VenueBUS();
            if (bus.isInUsed(id) && !confirmation) {
                ErrorObject error = new ErrorObject(STATUS_VALIDATION_ERROR);
                error.addError(Parameter.NAME, getMessagesFactory().itemIsInUsed("\"" + bus.getVenueById(id).getName() + "\""));
                error.setRequireConfirmation(true);
                sendResponse(error.toJson());
                return;
            }

            if (bus.deleteVenue(id)) {
                sendResponse(new ResponseObject((Object) null).toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void updateVenue(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String venueId = request.getParameter(Parameter.ID);
        String name = request.getParameter(Parameter.NAME);

        try {
            int id = Integer.parseInt(venueId);

            VenueBUS bus = new VenueBUS();
            Venue obj = bus.getVenueByName(name.trim()); 
            if (obj != null && obj.getId() != id) {
                ErrorObject error = new ErrorObject(STATUS_VALIDATION_ERROR);
                error.addError(Parameter.NAME, getMessagesFactory().areaIsDuplicated());
                sendResponse(error.toJson());
                return;
            }

            obj = bus.getVenueById(id);
            obj.setName(name);

            if (bus.save(obj)) {
                sendResponse(new ResponseObject(obj).toJson());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }
}
